/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Figure D.8, D.10 

Tasks: Heterogeneity by distance increase/decrease, 4 bins
	 
*/	

* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
*	 Prep Estimation *
********************************************************************************

	// relabel for output
	lab var turnout_urne 	"Polling Place Turnout"
	lab var turnout_pos_req "Mail-in Turnout"
	lab var turnout_tot_req "Total Turnout"

	// compute id for DISTANCE increase/decrease, 0 else
	// ind_dist_up := ids treated precincts that INCREASED dist 
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	// ind_dist_dn := ids treated precincts that DECREASED dist 
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"
	
	// ind_dist_up_high := ids treated precincts that INCREASED dist ABOVE MEDIAN
	cap drop tmp*
	sum del_street_dist if K==0 & del_street_dist>0, d	// median = .2624171
	gen tmp =  (del_street_dist>=r(p50)) 			if K==0
	
	bys sb_new (tmp): gen ind_dist_up_high = tmp[1]
	replace ind_dist_up_high = 0 					if missing(Ei)
	lab var ind_dist_up_high "=1 if dist increase above median in event, 0 else"
	
	// ind_dist_up_low := ids treated precincts that INCREASED dist BELOW MEDIAN
	gen ind_dist_up_low = ind_dist_up==1 & ind_dist_up_high==0
	lab var ind_dist_up_low "=1 if dist increase below median in event, 0 else"
	
	// ind_dist_dn_high := ids treated precincts that DECREASED dist ABOVE MEDIAN
	cap drop tmp*
	sum del_street_dist if K==0 & del_street_dist<0, d 
	gen tmp =  (del_street_dist<= r(p50)) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn_high = tmp[1]
	replace ind_dist_dn_high = 0 					if missing(Ei)
	lab var ind_dist_dn_high "=1 if dist decrease above median in event, 0 else"
	
	// ind_dist_dn_low := ids treated precincts that DECREASED dist BELOW MEDIAN
	gen ind_dist_dn_low = ind_dist_dn==1 & ind_dist_dn_high==0
	lab var ind_dist_dn_low "=1 if dist decrease below median in event, 0 else"

	
	// compute group ids: 4 groups ACCORDING to %addresses with dist increase	
	cap drop tmp*
	xtile 		tmp=street_increased if K==0, n(4)	
	bys sb_new: egen tmp_group = max(tmp) // ord. var 1-4 for 1/2/3/4th quartile of share addressses w/ dist increase
	
	// gen 4 dummy vars=1 if precints belongs to 1st/2/3/4th quartile, zero else
	tab tmp_group, gen(ind_shr_up) mis

	bys tmp_group: su del_street_dist street_increased if K==0

	
********************************************************************************
 // Heterogeneity: Increase/Decrease Distance (4 groups), (Figure D8) //
********************************************************************************		

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a1 = F`l'event *ind_dist_dn_high				// a := decrease above median
		lab var F`l'event_a1 "(N1-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b1 = F`l'event *ind_dist_up_high				// b := increase above median
		lab var F`l'event_b1 "(N1+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen 	F`l'event_a2 = F`l'event *ind_dist_dn_low				// a := decrease below median
		lab var F`l'event_a2 "(N2-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b2 = F`l'event *ind_dist_up_low				// b := increase below median
		lab var F`l'event_b2 "(N2+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b1+F`l'event_a1+F`l'event_b2+F`l'event_a2==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a1 = L`l'event *ind_dist_dn_high	// a := decrease
		lab var L`l'event_a1 "(N1-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b1 = L`l'event *ind_dist_up_high	// b:= increase
		lab var L`l'event_b1 "(N1+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen 	L`l'event_a2 = L`l'event *ind_dist_dn_low	// a := decrease
		lab var L`l'event_a2 "(N2-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b2 = L`l'event *ind_dist_up_low	// b:= increase
		lab var L`l'event_b2 "(N2+)x\hspace{.7cm}Reassignment (#t+`l'#)"

		assert  L`l'event_b1+L`l'event_a1+L`l'event_b2+L`l'event_a2==L`l'event
	}		
	
	// ORDER dummies
	order *event_a2, last
	order *event_b2, last
	order *event_b1, last
	order F1event*,last	
	
	lab var turnout_urne 	"{bf:Panel B.} Effect on Polling Place Turnout"
	lab var turnout_pos_req "{bf:Panel C.} Effect on Mail-in Turnout"
	lab var turnout_tot_req "{bf:Panel D.} Effect on Total Turnout"
	lab var del_street_dist "{bf:Panel A.} Change in Distance"
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg,clear
	foreach v of varlist del_street_dist turnout_urne turnout_pos_req turnout_tot_req  /*treat_simple*/ {
	
		reghdfe `v' F7event_a1-L7event_a1 F7event_a2-L7event_a2 ///
					F7event_b2-L7event_b2 F7event_b1-L7event_b1 ///
					F1event_a1 F1event_a2 F1event_b2 F1event_b1 ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				qui outreg,  $opt  keep(F4event_a1-L2event_a1  F4event_b1-L2event_b1 F4event_a2-L2event_a2  F4event_b2-L2event_b2) store(`v')

		
		estimates store `v'_a1
		estimates store `v'_a2	
		estimates store `v'_b2
		estimates store `v'_b1	

		
		// PLOT Outcome + save
		event_plot  `v'_a1 `v'_a2 `v'_b2 `v'_b1 , ///
		stub_lag(L#event_a1 L#event_a2 L#event_b2 L#event_b1) stub_lead(F#event_a1 F#event_a2 F#event_b2 F#event_b1) plottype(scatter) ciplottype(rcap) ///
		together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
		legend(pos(12) order(1 "Large distance decrease " 3 "Small distance decrease" 5 "Small distance increase" 7 "Large distance increase" ) size(small) col(2) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) /// 
		title("`:var lab `v''",nobox span bexpand justification(left) size(medium)) ///
			name(`v', replace))  ///
		lag_opt4(msymbol(S) 	msize(2.5pt) color(cranberry)) 	lag_ci_opt4(color(cranberry)) ///
		lag_opt3(msymbol(Sh) 	msize(2.5pt) color(maroon)) 	lag_ci_opt3(color(maroon)) ///
		lag_opt2(msymbol(Oh) 	msize(2.5pt) color(gray)) 		lag_ci_opt2(color(gray)) ///		
		lag_opt1(msymbol(O) 	msize(2.5pt) color(black)) 		lag_ci_opt1(color(black)) 	 

}

	* PLOT: FIGURE D8. Effect Heterogeneity by Change in Proximity to the Polling Location
	grc1leg del_street_dist turnout_urne turnout_pos_req turnout_tot_req, col(2)  xcommon pos(6) imargins(small)
	gr_edit .plotregion1.graph1.yaxis1.reset_rule -0.6 0.6 .3 , tickset(major) ruletype(range) 
	gr_edit .plotregion1.graph1.yaxis1.title.text = {}
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"Change in distance in km"'
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"(estimates)"'
	gr_edit .style.editstyle declared_ysize(4) editcopy

	graph export "$figures/Figure_D8_ES_het_by_distance_4clust.pdf", replace	


********************************************************************************
 // Heterogeneity: 4 groups by SHARE of addresses with distance up in K=0 (Figure D10)
********************************************************************************		

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a1 = F`l'event *ind_shr_up1				// a := decrease above median
		lab var F`l'event_a1 "(N1-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b1 = F`l'event *ind_shr_up4				// b:= increase
		lab var F`l'event_b1 "(N1+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen 	F`l'event_a2 = F`l'event *ind_shr_up2				// a := decrease below median
		lab var F`l'event_a2 "(N2-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b2 = F`l'event *ind_shr_up3				// b:= increase
		lab var F`l'event_b2 "(N2+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b1+F`l'event_a1+F`l'event_b2+F`l'event_a2==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a1 = L`l'event *ind_shr_up1	// a := decrease
		lab var L`l'event_a1 "(N1-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b1 = L`l'event *ind_shr_up4	// b:= increase
		lab var L`l'event_b1 "(N1+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen 	L`l'event_a2 = L`l'event *ind_shr_up2	// a := decrease
		lab var L`l'event_a2 "(N2-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b2 = L`l'event *ind_shr_up3	// b:= increase
		lab var L`l'event_b2 "(N2+)x\hspace{.7cm}Reassignment (#t+`l'#)"

		assert  L`l'event_b1+L`l'event_a1+L`l'event_b2+L`l'event_a2==L`l'event
	}		
	
	// ORDER dummies
	order *event_a2, last
	order *event_b2, last
	order *event_b1, last
	order F1event*,last	
	
	lab var turnout_urne 	"{bf:Panel B.} Effect on Polling Place Turnout"
	lab var turnout_pos_req "{bf:Panel C.} Effect on Mail-in Turnout"
	lab var turnout_tot_req "{bf:Panel D.} Effect on Total Turnout"
	lab var del_street_dist "{bf:Panel A.} Change in Distance"
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg,clear
	foreach v of varlist del_street_dist turnout_urne turnout_pos_req turnout_tot_req  /*treat_simple*/ {
	
		reghdfe `v' F7event_a1-L7event_a1 F7event_a2-L7event_a2 ///
					F7event_b2-L7event_b2 F7event_b1-L7event_b1 ///
					F1event_a1 F1event_a2 F1event_b2 F1event_b1 ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				qui outreg,  $opt  keep(F4event_a1-L2event_a1  F4event_b1-L2event_b1 F4event_a2-L2event_a2  F4event_b2-L2event_b2) store(`v')

		
		estimates store `v'_a1
		estimates store `v'_a2	
		estimates store `v'_b2
		estimates store `v'_b1	

		
		// PLOT Outcome + save
		event_plot  `v'_a1 `v'_a2 `v'_b2 `v'_b1 , ///
		stub_lag(L#event_a1 L#event_a2 L#event_b2 L#event_b1) stub_lead(F#event_a1 F#event_a2 F#event_b2 F#event_b1) plottype(scatter) ciplottype(rcap) ///
		together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
		legend(pos(12) order(1 "0-27%" 3 "29-69%" 5 "71-95%" 7 "96-100%" ) size(small) col(2) region(style(none)) title("% Addresses with distance increase in t=0")) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) /// 
		title("`:var lab `v''",nobox span bexpand justification(left) size(medium)) ///
			name(`v', replace))  ///
		lag_opt4(msymbol(S) 	msize(2.5pt) color(cranberry)) 	lag_ci_opt4(color(cranberry)) ///
		lag_opt3(msymbol(Sh) 	msize(2.5pt) color(maroon)) 	lag_ci_opt3(color(maroon)) ///
		lag_opt2(msymbol(Oh) 	msize(2.5pt) color(gray)) 		lag_ci_opt2(color(gray)) ///		
		lag_opt1(msymbol(O) 	msize(2.5pt) color(black)) 		lag_ci_opt1(color(black)) 	 

}

	* PLOT: FIGURE D10. Effect Heterogeneity by Share of Addresses with Distance Increase
	grc1leg del_street_dist turnout_urne turnout_pos_req turnout_tot_req, col(2)  xcommon pos(6) imargins(small)
	gr_edit .plotregion1.graph1.yaxis1.reset_rule -0.6 0.6 .3 , tickset(major) ruletype(range) 
	gr_edit .plotregion1.graph1.yaxis1.title.text = {}
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"Change in distance in km"'
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"(estimates)"'
	gr_edit .style.editstyle declared_ysize(4) editcopy

	graph export "$figures/Figure_D10_ES_het_by_distance_shrHHdist4.pdf", replace	
